#!/bin/bash
#
# Routines for managing system context information, including high-level
# accessor and mutator routines.
#
# Author:   Dr. Mike Murphy (mmurphy2@coastal.edu)
# Revision: 19 November 2013
#
#   Copyright 2012-2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#


# Default context backend is the simple filesystem backend, using the context
# subdirectory of the Pulley state directory.
declare -g CONTEXT_BACKEND="filesystem:${P_STATEDIR}/context"


## p_context_lazy_load
##
## Loads the functions that implement the CONTEXT BACKEND, if those functions
## are not already loaded. Returns 0 if successful, non-zero otherwise.
##
function p_context_lazy_load() {
	local backend=$(echo "${CONTEXT_BACKEND}" | awk -F ':' '{print $1}')
	
	lazy_load p_context_backend_${backend}_get "backends" "${backend}.backend"
	return $?
}


## p_context_validate_path <path>
##
## Validates a (potentially user-requested) context path to be sure that it
## does not contain illegal characters, which could cause undesired effects
## such as writing or deleting context outside the context directory. Valid
## context paths contain Latin characters a-z and A-Z, digits 0-9,
## underscores, and forward slashes to enable context hierarchies.
##
## <path>   Context path to validate
##
## Returns 0 if the path is valid, 1 if the path is invalid.
##
function p_context_validate_path() {
	local chk
	local status=0
	
	chk=$(echo "${base}" | sed s/[a-zA-Z0-9_/]*//)
	
	if [[ -n "${chk}" ]]; then
		# We have illegal characters in the identifier name
		status=1
	fi
	
	return ${status}
}


## get_context <path> [default value]
##
## Retrieves and echoes a context item stored at <path>. If the context
## item does not exist and [default value] is empty, does not echo anything;
## otherwise echoes the [default value] if context is not set. Returns 0
## normally, 2 if <path> is invalid, or another non-zero value if an error
## occurs in the context back-end.
##
function get_context() {
	local status=1
	local backend=$(echo "${CONTEXT_BACKEND}" | awk -F ':' '{print $1}')
	local value=""
	
	if p_context_lazy_load; then
		value=$(p_context_backend_${backend}_get "$1")
		status=$?
	fi
	
	[[ -z "${value}" ]] && value="$2"
	[[ -n "${value}" ]] && echo "${value}"
	
	return ${status}
}


## set_context <path> <value>
##
## Sets the context item stored at <path> to <value>. Returns 0 upon success,
## non-zero if an error occurs.
##
function set_context() {
	local backend=$(echo "${CONTEXT_BACKEND}" | awk -F ':' '{print $1}')
	local status=1
	
	if p_context_lazy_load; then
		p_context_backend_${backend}_set "$1"
		status=$?
	fi
	
	return ${status}
}


## clear_context <path>
##
## Clears the context item stored at <path>. Returns 0 upon success, non-zero
## if an error occurs.
##
function clear_context() {
	local backend=$(echo "${CONTEXT_BACKEND}" | awk -F ':' '{print $1}')
	local status=1
	
	if p_context_lazy_load; then
		p_context_backend_${backend}_clear "$1"
		status=$?
	fi
	
	return ${status}
}
